//
//  JYStatusCell.swift
//  01.weibo
//
//  Created by 谢玉锋 on 16/7/16.
//  Copyright © 2016年 itheima.cn. All rights reserved.
//

import UIKit
import SnapKit

class JYStatusCell: UITableViewCell {

    /// 记录之前bottomView的top约束
    var bottomViewTopCons: Constraint?
    
    var status: JYStatus? {
        //属性监听器
        // 当控制器给cell设置模型后,来做操作
        // 将微博模型传递给originalView
        didSet {
        //赋值给原创View
        originalView.status = status
        // 先卸载掉之前的bottomView的顶部约束
        bottomViewTopCons?.uninstall()
            
        //赋值给转发View
        if let retweeted_status = status?.retweeted_status {
            //即是隐藏 约束还是在的, 所以要将他卸载掉,之后再从新赋值
            retweetView.hidden = false
            retweetView.retweeted_status = retweeted_status
            
            bottomView.snp_makeConstraints(closure: { (make) -> Void in
                bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            })
        }else {
            //如果没有转发
            retweetView.hidden = true
            bottomView.snp_makeConstraints(closure: { (make) -> Void in
                bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
            })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //注册方式创建cell , 会调用 一下方法: //注册方式创建UITableViewCell, 调用这个函数:
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        prepareUI ()
        
        
    }
    //准备子控件
    private func prepareUI () {
    
        //添加子控件  content  [kən'tent] 内容，目录；满足；容量
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        //子控件 originalView 添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.right.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
//            make.height.equalTo( Int(arc4random_uniform(200)) + 50)
        }
        //子控件 originalView 添加约束
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            // TODO: 测试
            // 实际上被转发view的高度需要参考自己被转发的配图       //所以这里不不清楚高度的
//            make.height.equalTo(CGFloat(150))

        }
        
        //子控件 bottomView 添加约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            // bottomView的顶部参照被转发view的底部   (这里在数据回来之前调用,暂且参照retweetView ,当数据回来后 第28行 ,再重新设置约束)
            bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(CGFloat(37))
        }
        
        // cell的高度根据里面子控件的约束来自动调整
        // cell的子控件约束要正确,还需要约束contentView的约束(最主要的是高度约束),系统会让cell取contentView的高度
        //添加contentView的约束 (由里面子控件添加约束--> 到contentView添加约束 --> cell根据contentView调整高度(第一,第二步做的))
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.right.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
    }
    
    
    

    //懒加载
    private lazy var originalView: JYStatusOriginalView = JYStatusOriginalView()
    
    private lazy var bottomView: JYStatusBottomView = JYStatusBottomView()
    /// 被转发的view
    private lazy var retweetView: JYStatusRetweetView = JYStatusRetweetView()
}










